package com.wmh.baseservice.skd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.service.TokenService;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.skd.pojo.SkdCollect;
import com.wmh.baseservice.skd.pojo.SkdGroupBuyingActivities;
import com.wmh.baseservice.skd.pojo.vo.CollectionOrBrowsingRespVO;
import com.wmh.baseservice.skd.pojo.vo.FollowGroupNumAndBrowseNumRespVO;
import com.wmh.baseservice.skd.service.SkdCollectService;
import com.wmh.baseservice.skd.service.SkdGroupBuyingActivitiesService;
import com.wmh.baseservice.skd.service.SkdLeaderInfoService;
import com.wmh.baseservice.user.pojo.vo.UsInfoRespVO;
import com.wmh.baseservice.user.service.UsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.*;

@Api(tags = "收藏相关接口")
@RestController
@RequestMapping(value = "collect", produces = "text/plain;charset=utf-8")
@Slf4j
public class CollectController {
    @Resource
    private TokenService tokenService;
    @Resource
    private UsInfoService usInfoService;
    @Resource
    private SkdLeaderInfoService skdLeaderInfoService;
    @Resource
    private SkdCollectService skdCollectService;
    @Resource
    private SkdGroupBuyingActivitiesService skdGroupBuyingActivitiesService;

    /**
     * #0066 收藏团购活动
     * @param groupBuyingActivityId 团购活动id
     * @return 返回是否收藏成功信息
     */
    @ApiOperation("收藏团购活动")
    @PostMapping
    public String addCollect(@NotNull(message = "未提交团购活动id") Long groupBuyingActivityId) {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        SkdGroupBuyingActivities res = skdGroupBuyingActivitiesService.getById(groupBuyingActivityId);
        if (Objects.isNull(res)) {
            return ReturnBody.error("团购活动不存在");
        }
        SkdCollect skdCollect = new SkdCollect();
        skdCollect.setUserId(usId);
        skdCollect.setGroupBuyingActivityId(groupBuyingActivityId);
        skdCollectService.save(skdCollect);
        return ReturnBody.success("收藏成功!");
    }

    /**
     * #0067 取消收藏团购活动
     * @param groupBuyingActivityId 团购活动id
     * @return 返回是否取消收藏成功信息
     */
    @ApiOperation("取消收藏团购活动")
    @DeleteMapping
    public String cancelCollectionGroupBuyingActivity(@NotNull(message = "未提交团购活动id") Long groupBuyingActivityId) {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        SkdGroupBuyingActivities res = skdGroupBuyingActivitiesService.getById(groupBuyingActivityId);
        if (Objects.isNull(res)) {
            return ReturnBody.error("团购活动不存在");
        }
        QueryWrapper<SkdCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", usId).eq("group_buying_activity_id", groupBuyingActivityId);
        skdCollectService.remove(queryWrapper);
        return ReturnBody.success("取消收藏成功!");
    }

    /**
     * #0068 查看收藏的团购活动
     * @return 返回收藏的团购活动信息列表
     */
    @ApiOperation("查看收藏的团购活动")
    @GetMapping
    public String checkCollect() {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        List<CollectionOrBrowsingRespVO> collectionOrBrowsingRespVOList = new ArrayList<>();
        List<SkdCollect> SkdCollectList = skdCollectService.getByUserId(usId);
        for (SkdCollect skdCollect : SkdCollectList) {
            // 根据id获取团购活动
            SkdGroupBuyingActivities groupBuyingActivity = skdGroupBuyingActivitiesService.getById(skdCollect.getGroupBuyingActivityId());
            // 获取发起团购活动的用户昵称和头像地址
            UsInfoRespVO userInfo = usInfoService.getAvatarAndNickByUsId(groupBuyingActivity.getUserId());
            // 获取价格范围
            String priceRange = skdGroupBuyingActivitiesService.getPriceRangeFromGoodsSnapshot(groupBuyingActivity.getGoodsSnapshot());
            // 获取商品图片地址
            List<String> goodsImages = skdGroupBuyingActivitiesService.getGoodsImagesFromGoodsSnapshot(groupBuyingActivity.getGoodsSnapshot());
            // 获取商品视频地址
            String goodsVideo = skdGroupBuyingActivitiesService.getGoodsVideoFromGoodsSnapshot(groupBuyingActivity.getGoodsSnapshot());
            // 获取跟团和浏览人数
            FollowGroupNumAndBrowseNumRespVO followGroupNumAndBrowseNum = skdLeaderInfoService.getFollowGroupNumAndBrowseNumByGroupBuyingActivityId(groupBuyingActivity.getId());

            CollectionOrBrowsingRespVO collectionOrBrowsingRespVO = new CollectionOrBrowsingRespVO();

            collectionOrBrowsingRespVO
                    .setId(skdCollect.getId())
                    .setGroupBuyingActivityId(skdCollect.getGroupBuyingActivityId())
                    .setAvatarUrl(userInfo.getAvatar())
                    .setNick(userInfo.getNick())
                    .setGroupBuyingActivityTitle(groupBuyingActivity.getTitle())
                    .setGroupBuyingActivityPriceRange(priceRange)
                    .setGoodsImageUrls(goodsImages)
                    .setGoodsVideoUrl(goodsVideo)
                    .setFollowGroupNum(followGroupNumAndBrowseNum.getFollowGroupNum())
                    .setBrowseNum(followGroupNumAndBrowseNum.getBrowseNum())
                    .setIsEnd(groupBuyingActivity.getIsEnd())
                    .setDate(skdCollect.getCreateTime());

            collectionOrBrowsingRespVOList.add(collectionOrBrowsingRespVO);
        }
        return ReturnBody.success(collectionOrBrowsingRespVOList);
    }

    /**
     * #0069 搜索收藏的团购名称
     * @param searchName 搜索名称
     * @return 返回符合条件的团购活动信息列表
     */
    @ApiOperation("搜索收藏的团购名称")
    @GetMapping("search")
    public String searchCollect(@NotBlank(message = "未提交团购名称") String searchName) {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        List<CollectionOrBrowsingRespVO> collectionOrBrowsingRespVOList = new ArrayList<>();
        List<SkdCollect> skdCollectList = skdCollectService.searchCollect(searchName, usId);
        for (SkdCollect skdCollect : skdCollectList) {
            // 根据id获取团购活动
            SkdGroupBuyingActivities groupBuyingActivity = skdGroupBuyingActivitiesService.getById(skdCollect.getGroupBuyingActivityId());
            // 获取发起团购活动的用户昵称和头像地址
            UsInfoRespVO userInfo = usInfoService.getAvatarAndNickByUsId(groupBuyingActivity.getUserId());
            // 获取价格范围
            String priceRange = skdGroupBuyingActivitiesService.getPriceRangeFromGoodsSnapshot(groupBuyingActivity.getGoodsSnapshot());
            // 获取商品图片地址
            List<String> goodsImages = skdGroupBuyingActivitiesService.getGoodsImagesFromGoodsSnapshot(groupBuyingActivity.getGoodsSnapshot());
            // 获取商品视频地址
            String goodsVideo = skdGroupBuyingActivitiesService.getGoodsVideoFromGoodsSnapshot(groupBuyingActivity.getGoodsSnapshot());
            // 获取跟团和浏览人数
            FollowGroupNumAndBrowseNumRespVO followGroupNumAndBrowseNum = skdLeaderInfoService.getFollowGroupNumAndBrowseNumByGroupBuyingActivityId(groupBuyingActivity.getId());

            CollectionOrBrowsingRespVO collectionOrBrowsingRespVO = new CollectionOrBrowsingRespVO();

            collectionOrBrowsingRespVO
                    .setId(skdCollect.getId())
                    .setGroupBuyingActivityId(skdCollect.getGroupBuyingActivityId())
                    .setAvatarUrl(userInfo.getAvatar())
                    .setNick(userInfo.getNick())
                    .setGroupBuyingActivityTitle(groupBuyingActivity.getTitle())
                    .setGroupBuyingActivityPriceRange(priceRange)
                    .setGoodsImageUrls(goodsImages)
                    .setGoodsVideoUrl(goodsVideo)
                    .setFollowGroupNum(followGroupNumAndBrowseNum.getFollowGroupNum())
                    .setBrowseNum(followGroupNumAndBrowseNum.getBrowseNum())
                    .setIsEnd(groupBuyingActivity.getIsEnd())
                    .setDate(skdCollect.getCreateTime());

            collectionOrBrowsingRespVOList.add(collectionOrBrowsingRespVO);
        }
        return ReturnBody.success(collectionOrBrowsingRespVOList);
    }
}
